﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace CapstoneWinPhone.Data
{
    public class Trip
    {
        const double EarthRadius = 3958.75587;
        public LocationCollection _locations { get; set; }
        public string _description { get; set; }
        public double TotalMileage { get; private set; }
        public DateTime date { get; set; }

        public Trip()
        {

        }

        public Trip(LocationCollection locations, string description)
        {
            _locations = locations;
            _description = description;
            date = DateTime.Now;
            CalculateTotalMileage();
        }

        private void CalculateTotalMileage()
        {
            TotalMileage = 0.0;
            if (_locations.Count >= 2)
            {
                int k = 1;
                for (int i = 0; i < _locations.Count - 1; i++)
                {
                    GeoCoordinate first = _locations[i];
                    GeoCoordinate second = _locations[k];
                    double dLat = toRadian(second.Longitude - first.Longitude);
                    double dLon = toRadian(second.Latitude - first.Latitude);
                    double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Cos(this.toRadian(first.Longitude)) * Math.Cos(this.toRadian(second.Longitude)) *
                        Math.Sin(dLon / 2) * Math.Sin(dLon / 2);

                    double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
                    double d = EarthRadius * c;
                    TotalMileage += d;
                }
            }
        }
        private double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }

        public override string ToString()
        {
            return date.ToShortDateString() + " - " + TotalMileage.ToString("#.##") + " miles" ;
        }
    }
}
